﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace LinkCard.AdminCP.Company
{
    public partial class company_all_published : System.Web.UI.Page
    {
        public string sKeyword = "";
        public int nIndustryID = 0;
        public int nLevelID = 0;


        private int _totalRecords;
        private int nPageSize = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            //else
            //{
            //    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
            //    {
            //        if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
            //        {
            //            Response.Redirect("/message_box.aspx?why=1&msg=5");
            //        }
            //    }
            //}

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["keyword"] != null) { sKeyword = Request.QueryString["keyword"].Trim(); }
                //if (Request.QueryString["IndustryID"] != null) { nIndustryID = ConvertUtility.ToInt32(Request.QueryString["IndustryID"].Trim()); }
                //if (Request.QueryString["LevelID"] != null) { nLevelID = ConvertUtility.ToInt32(Request.QueryString["LevelID"].Trim()); }

                DataBindPaging(sKeyword, nIndustryID, nLevelID);
                loadFormSearch(nIndustryID, nLevelID);
            }

        }

        private void DataBindPaging(string keyword, int industryID, int levelID)
        {

            try
            {
                string sFilter = "";
                if (keyword != "" && keyword != "Tên công ty") sFilter = sFilter + " AND CompanyName like N'%" + keyword + "%'";
                //if (industryID != 0) sFilter = sFilter + " AND JobID in (Select JobID from Job_Industrial where IndustryID ="+industryID+")";
                //if (levelID != 0) sFilter = sFilter + " AND Jobs.LevelID = "+levelID+"";

                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }



                DataTable dt = DataHelper.LoadPageInSQL("CompanyList", curpage, nPageSize, ref _totalRecords, new SqlParameter("@Filter", sFilter));

                if (dt.Rows.Count > 0)
                {
                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else
                    {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }

                    if (nPageCount < 2) divPager.Visible = false;

                    PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);

                    msgList.DataSource = dt;

                    msgList.DataBind();
                    msgNoData.Visible = false;

                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["CompanyID"].ToString();

            //var lblJobIndustries = (Label)e.Item.FindControl("JobIndustries");
            //var dt = DataHelper.SelectTable("Select IndustryName from Industrial where IndustryID in (Select IndustryID from Job_Industrial where JobID=" + row["JobID"].ToString() + " )",false);
            //for (int i = 0; i < dt.Rows.Count; i++) {
            //    lblJobIndustries.Text += dt.Rows[i]["IndustryName"].ToString() + ", ";
            //}

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int nAction = int.Parse(ActionOption.Value);
            if (nAction == 1)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";

                string sSQL = "delete Company where CompanyID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                Response.Redirect("/Company/company_all_published.aspx");
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            sKeyword = txtSearch.Value.Trim();
            //nIndustryID = int.Parse(JobIndustries.SelectedValue);
            //nLevelID = int.Parse(JobLevel.SelectedValue);

            Response.Redirect("company_all_published.aspx?txtSearch="+sKeyword+"");
        }

        protected void loadFormSearch(int industryID, int levelID)
        {
           
            //DataTable dt;
            //dt = DataHelper.SelectTable("SELECT IndustryID, IndustryName FROM Industrial ORDER BY IndustryName", false);
            //JobIndustries.DataSource = dt;
            //JobIndustries.DataTextField = "IndustryName";
            //JobIndustries.DataValueField = "IndustryID";
            //JobIndustries.DataBind();
            //JobIndustries.Items.Insert(0, new ListItem("Tất cả ngành nghề", "0"));
            //JobIndustries.SelectedValue = industryID.ToString();

            //dt = DataHelper.SelectTable("SELECT JobLevelID, JobLevelName FROM JobLevel ORDER BY JobLevelName", false);
            //JobLevel.DataSource = dt;
            //JobLevel.DataTextField = "JobLevelName";
            //JobLevel.DataValueField = "JobLevelID";
            //JobLevel.DataBind();
            //JobLevel.Items.Insert(0, new ListItem("Tất cả cấp bậc", "0"));
            //JobLevel.SelectedValue = levelID.ToString();

        }


    }
}